package com.sunny.hp.common.poi;

import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

// Source File Name:   ExcelPoiUtil.java
public class ExcelPoiUtil{

	public ExcelPoiUtil() {
		super();
	}

	// static Font thisFont = null;
	/**
	 * �÷� Header
	 * 
	 * @param wb
	 * @param row
	 * @param column
	 * @param data
	 * @param halign
	 * @param valign
	 */
	public static Cell createHeaderCell(Workbook wb, Row row, short column,
			String data, short halign, short valign, Font font) {
		if (halign == -1)
			halign = XSSFCellStyle.ALIGN_CENTER;// ALIGN_LEFT;
		if (valign == -1)
			valign = XSSFCellStyle.VERTICAL_CENTER;// VERTICAL_BOTTOM;
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(halign);
		cellStyle.setVerticalAlignment(valign);

		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		cell.setCellStyle(cellStyle);

		cellStyle.setFont(font);

		return cell;

	}
	
	/**
	 * �÷� Header
	 * 
	 * @param wb
	 * @param row
	 * @param column
	 * @param data
	 * @param halign
	 * @param valign
	 */
	public static Cell createHeaderCell(Workbook wb, Row row, short column,
			String data, CellStyle cellStyle) {
		
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		

		cell.setCellStyle(cellStyle);



		return cell;

	}

	/**
	 * �÷� Title
	 * 
	 * @param wb
	 * @param row
	 * @param column
	 * @param data
	 * @param halign
	 * @param valign
	 */
	public static Cell createTitleCell(Workbook wb, Row row, short column,
			String data, short halign, short valign, Font font) {
		if (halign == -1)
			halign = XSSFCellStyle.ALIGN_CENTER;// ALIGN_LEFT;
		if (valign == -1)
			valign = XSSFCellStyle.VERTICAL_CENTER;// VERTICAL_BOTTOM;
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(halign);
		cellStyle.setVerticalAlignment(valign);

		cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		// ���� Border�ֱ�
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);

		cell.setCellStyle(cellStyle);

		cellStyle.setFont(font);

		// Fonts are set into a style so create a new one to use.

		return cell;

	}

	/**
	 * Excel Print
	 * 
	 * @param sheet
	 */
	public static void settingExcelPrint(Sheet sheet) {
		// ��¼���?
		// HSSFPrintSetup hps = sheet.getPrintSetup();
		PrintSetup hps = sheet.getPrintSetup();
		// ��������
		hps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		// ��¹��⼳��?
		hps.setLandscape(true);// true,����
		// ��º�������?
		hps.setScale((short) 100);

		// footer�� ��������ȣ ����
		// HSSFFooter footer = sheet.getFooter();
		Footer footer = sheet.getFooter();
		footer.setCenter(HSSFFooter.page() + "/" + HSSFFooter.numPages());

		// Header,Footer ���� �ֱ�
		hps.setHeaderMargin((double) 0.1);
		hps.setFooterMargin((double) 0.1);// ������ 0.3

		// ����,�¿� ���� �ֱ�
		sheet.setMargin(HSSFSheet.TopMargin, (double) .30);// ������ 0.8
		sheet.setMargin(HSSFSheet.BottomMargin, (double) .30);// ������ 0.8
		sheet.setMargin(HSSFSheet.LeftMargin, (double) .20);// ������ 0.5
		sheet.setMargin(HSSFSheet.RightMargin, (double) .20);

	}

	/**
	 * cell �Ӽ�  : Border ,Align
	 * 
	 * @param wb
	 *            the workbook
	 * @param row
	 *            the row to create the cell in
	 * @param column
	 *            the column number to create the cell in
	 * @param halign
	 *            the horizontal alignment for the cell.
	 */
	public static Cell createCell(Workbook wb, Row row, short column,
			String data, short halign, short valign, Font font) {
		if (halign == -1)
			halign = XSSFCellStyle.ALIGN_LEFT;
		if (valign == -1)
			valign = XSSFCellStyle.VERTICAL_CENTER;// VERTICAL_BOTTOM;
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(halign);
		cellStyle.setVerticalAlignment(valign);

		cell.setCellStyle(cellStyle);

		cellStyle.setFont(font);
		return cell;
	}
	
	/**
	 * ���ۼ� : Border ,Align
	 * 
	 * @param wb
	 *            the workbook
	 * @param row
	 *            the row to create the cell in
	 * @param column
	 *            the column number to create the cell in
	 * @param halign
	 *            the horizontal alignment for the cell.
	 */
	public static Cell createCell(Workbook wb, Row row, short column,
			String data, CellStyle cellStyle) {
		
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		
		cell.setCellStyle(cellStyle);

		return cell;
	}
	
	/**
	 * ���Ӽ� : Border ,Align
	 * 
	 * @param wb
	 *            the workbook
	 * @param row
	 *            the row to create the cell in
	 * @param column
	 *            the column number to create the cell in
	 * @param halign
	 *            the horizontal alignment for the cell.
	 */
	public static Cell createCell(Workbook wb, Row row, short column,
			double data, short halign, short valign, Font font) {
		if (halign == -1)
			halign = XSSFCellStyle.ALIGN_LEFT;
		if (valign == -1)
			valign = XSSFCellStyle.VERTICAL_CENTER;// VERTICAL_BOTTOM;
		Cell cell = row.createCell(column);
		cell.setCellValue(data);

		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(halign);
		cellStyle.setVerticalAlignment(valign);

		cell.setCellStyle(cellStyle);

		cellStyle.setFont(font);
		return cell;
	}
}
